﻿@inherits LayoutComponentBase
@inject NavigationManager navigationManager
@inject AppState appState
@inject IStringLocalizer<Global> L
@attribute [Authorize(Policies.IsAdmin)]

<MatDrawerContainer Style="width: 100vw; height: 100vh;" Class="@bbDrawerClass">
    <MatDrawer @bind-Opened="@_navMenuOpened">
        <header class="drawer-header">
            <div class="drawer-logo">
                <img alt="@appState.AppName" class="logo-img" src="_content/BlazorBoilerplate.Theme.Material/images/logo-dark.svg" title="@appState.AppName">
                <a class="miniHover" href="/">@appState.AppName</a>
            </div>
            <UserProfile />
        </header>
        <AdminNavMenu />
    </MatDrawer>
    <MatDrawerContent>
        <MatAppBarContainer Style="display: flex; flex-direction: column; min-height: 100vh;">
            <MatAppBar Fixed="true">
                <MatAppBarRow>
                    <MatAppBarSection>
                        <MatAppBarTitle>
                        </MatAppBarTitle>
                        <div class="hidden-mdc-down">
                            <MatTooltip Tooltip="@L["AppHoverNavToggle"]">
                                <MatIconButton Class="navToggle" Icon="menu" ToggleIcon="menu" OnClick="@((e) => NavToggle())" RefBack="@context"></MatIconButton>
                            </MatTooltip>
                            <MatTooltip Tooltip="@L["AppHoverNavMinimize"]">
                                <MatIconButton Class="navToggle" Icon="format_indent_decrease" ToggleIcon="format_indent_increase" OnClick="@((e) => NavMinify())" RefBack="@context"></MatIconButton>
                            </MatTooltip>
                        </div>
                    </MatAppBarSection>
                    <MatAppBarSection Align="@MatAppBarSectionAlign.End">
                        <SelectCulture />
                        <Login />
                    </MatAppBarSection>
                </MatAppBarRow>
            </MatAppBar>
            <MatAppBarContent Style="flex: 1; display: flex; flex-direction: column;">
                <Breadcrumbs RootLinkTitle="@appState.BreadCrumbHome"></Breadcrumbs>
                <section class="container-fluid" style="flex: 1;">
                    @Body
                </section>
                <footer class="page-footer">
                    <TenantInfo />
                </footer>
            </MatAppBarContent>
        </MatAppBarContainer>
    </MatDrawerContent>
</MatDrawerContainer>

@code {
    bool _navMenuOpened = true;
    bool _navMinified = false;
    public string bbDrawerClass = "";

    private void CallLogin()
    {
        var returnUrl = navigationManager.ToBaseRelativePath(navigationManager.Uri);

        navigationManager.NavigateTo($"{BlazorBoilerplate.Constants.Settings.LoginPath}/{returnUrl}", forceLoad: false);
    }

    [CascadingParameter]
    Task<AuthenticationState> authenticationStateTask { get; set; }

    protected override async Task OnInitializedAsync()
    {
        var user = (await authenticationStateTask).User;

        if (user.Identity.IsAuthenticated)
        {
            var profile = await appState.GetUserProfile();

            if (profile == null)
                profile = new BlazorBoilerplate.Shared.Dto.Db.UserProfile();

            _navMenuOpened = profile.IsNavOpen;
            _navMinified = profile.IsNavMinified;
        }
    }

    void NavToggle()
    {
        _navMenuOpened = !_navMenuOpened;
        if (_navMenuOpened)
        {
            bbDrawerClass = "full";
        }
        else
        {
            bbDrawerClass = "closed";
        }

        this.StateHasChanged();
    }

    void NavLock()
    {
        //Todo Lock Nav
    }

    void NavMinify()
    {
        _navMinified = !_navMinified;

        if (!_navMenuOpened)
        {
            _navMinified = true;
        }

        if (_navMinified)
        {
            bbDrawerClass = "mini";
            _navMenuOpened = true;
        }
        else if (_navMenuOpened)
        {
            bbDrawerClass = "full";
        }

        _navMenuOpened = true;
        this.StateHasChanged();
    }
}